<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录账号" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入登录账号"
          clearable
          @input="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="uname">
        <el-input
          v-model="queryParams.uname"
          placeholder="请输入用户名"
          clearable
          @input="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色"
          clearable
          @input="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属公司" prop="ofCompany">
        <el-select v-model="queryParams.ofCompany" placeholder="请选择所属公司" >
          <el-option v-for="i in companyList" :label="i.companyName" :value="i.id" :key="i.id" />
        </el-select>
      </el-form-item>
      <el-form-item >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="uid" />
      <el-table-column label="登录账号" align="center" prop="loginName" />
      <el-table-column label="姓名" align="center" prop="uname" />
      <el-table-column label="角色" align="center" prop="roleName">
        <template slot-scope="scope">
          <span v-if="scope.row.roleName=='2'">财务</span>
          <span v-if="scope.row.roleName=='3'">管理员</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center" prop="ofCompany">
        <template slot-scope="scope">
          <span>{{scope.row.companyName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="登录账号" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="用户密码" prop="upass">
          <el-input v-model="form.upass" disabled placeholder="新增、修改 默认123456" />
        </el-form-item>
        <el-form-item label="姓名" prop="uname">
          <el-input v-model="form.uname" placeholder="请输入用户名，如张三" />
        </el-form-item>
        <el-form-item label="角色" prop="roleName">
          <el-select v-model="form.roleName" disabled placeholder="请选择角色名称" >
            <el-option v-for="i in roleList" v-if="i.roleId==2 || i.roleId==3" :label="i.roleName" :value="i.roleId" :key="i.roleId" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属公司" prop="ofCompany">
          <el-select v-model="form.ofCompany" disabled placeholder="请选择所属公司" >
            <el-option v-for="i in companyList" :label="i.companyName" :value="i.id" :key="i.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listUser, getUser, delUser, addUser, updateUser } from "@/api/system/tuser";
  import { listRole } from "@/api/system/role";
  import { listCompany } from "@/api/system/company";
  import { getInfo } from "@/api/login";
  export default {
    name: "User",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        userList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          loginName: null,
          upass: null,
          uname: null,
          roleName: null,
          apiKey: null,
          createtime: null,
          modifytime: null,
          avatar: null,
          ofCompany: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          loginName: [
            { required: true, message: "登录账号不能为空", trigger: "blur" },
            {
              type: "email",
              message: "请输入正确的邮箱地址",
              trigger: ["blur", "change"]
            }
          ],
          uname: [
            { required: true, message: "用户名，如张三不能为空", trigger: "blur" }
          ],
          roleName: [
            { required: true, message: "角色名称，管理员和财务不能为空", trigger: "blur" }
          ],
          ofCompany: [
            { required: true, message: "所属公司不能为空", trigger: "blur" }
          ]
        },
        roleList:null,
        companyList:null,
      };
    },
    created() {
      this.getList();
      this.getData();
    },
    methods: {
      getData(){
        listRole().then(rep=>{
          this.roleList=rep.rows;
        })
        listCompany().then(rep=>{
          this.companyList=rep.rows;
        })
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        getInfo().then(data=>{
          this.queryParams.ofCompany=data.tuser.ofCompany;
          listUser(this.queryParams).then(response => {
            this.userList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        })

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          uid: null,
          loginName: null,
          upass: null,
          uname: null,
          roleName: null,
          apiKey: null,
          createtime: null,
          modifytime: null,
          avatar: null,
          ofCompany: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.uid)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加";
        getInfo().then(data=>{
          this.form.roleName=parseInt(data.tuser.roleName);
          this.form.ofCompany=data.tuser.ofCompany
        })

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const uid = row.uid || this.ids
        getUser(uid).then(response => {
          this.form = response.data;
          this.form.upass='';
          this.form.roleName=parseInt(this.form.roleName)
          this.open = true;
          this.title = "修改";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.uid != null) {
              this.form.roleId=this.form.roleName;
              this.form.upass='123456';
              updateUser(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.upass='123456';
              this.form.roleId=this.form.roleName;
              addUser(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const uids = row.uid || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + uids + '"的数据项？').then(function() {
          return delUser(uids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
